package com.dream.wukong.common.core.importexport.csv.impl;

import com.dream.wukong.common.core.importexport.csv.annotation.CsvConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.PatternMatchUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: wuyichao
 * @Description:
 * @Date: Created in 16:04 2018/9/17.
 * @Modified:
 */
@CsvConverter
public abstract class BaseConverter {

    private static final Logger logger = LoggerFactory.getLogger(BaseConverter.class);

    /**
     * 是否导出
     * @return
     */
    protected static boolean isExportReq() {
        HttpServletRequest request = getRequest();
        if(null == request){
            return false;
        }
        String uri = request.getRequestURI();
        logger.debug("export import url :" + uri);
        return PatternMatchUtils.simpleMatch("*/export", uri )||uri.endsWith("/sqlexport");
    }

    /**
     * 是否导入
     * @return
     */
    protected static boolean isImportReq() {
        HttpServletRequest request = getRequest();
        if(null == request){
            return false;
        }
        String uri = request.getRequestURI();
        logger.debug("export/import url :" + uri);

        return PatternMatchUtils.simpleMatch("*/import", uri )||uri.endsWith("/csv/exportInValidRecord")||uri.endsWith("/sqlimport") ;
    }

    /**
     * 获取request信息，从而获得用户登录信息及浏览器访问信息
     *
     * @return
     */
    protected static HttpServletRequest getRequest() {
        HttpServletRequest request = CsvImportImpl.getRequest();
//		if(request==null) {
//			request = WebUtil.getRequest();
//		}
        return request;
    }

    /**
     * @return
     */
    public static String allwaysTrue(Object param) {
        logger.debug("param :" + param);
        return "true" ;
    }

    /**
     * @return
     */
    public static String allwaysFalse(Object param) {
        logger.debug("param :" + param);
        CsvImportImpl.setErrmsg("始终验证不通过");
        return "false" ;
    }


}
